/*****************************************************************************
	SNEGA UTILITIES 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/snega-utilities/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package org.sng.util.web.elmt;

import java.util.*;

import javax.servlet.http.*;

import org.sng.util.slog.*;
import org.sng.util.var.*;

public class NodeTextField extends Node {
	
	public NodeTextField()
	{
		super();
	}
	
	public NodeTextField(int n)
	{
		super(n);
	}
	
	public NodeTextField(String id)
	{
		super();
		
		this.PushProperty("id", id);
	}
	
	public NodeTextField Create()
	{
		return new NodeTextField();
	}
	
	public NodeTextField Create(int n)
	{
		return new NodeTextField(n);
	}
	
	public String GetType()
	{
		return "textfield";
	}
	
	public NodeTextField Create(String id, RuntimeEnvironment re)
	{
		HttpServletRequest request = re.GetRequest();
		
		NodeTextField o = new NodeTextField();
		
		o.PushProperty("id", new SString(id));
		
		/*try {
			o.m_properties.put("value", new SString(new String(request.getParameter(id + "_value").getBytes("ISO-8859-1"), "UTF-8")));
		} catch (UnsupportedEncodingException e) {
			o.m_properties.put("value", new SString(""));
			e.printStackTrace();
		}*/

		o.PushProperty("value", request.getParameter(id + "_value"));
		
		String postback = request.getParameter(id + "_postback");
		if (postback != null)
			o.PushProperty("postback", postback);
		
		return o;
	}
	
	public StringBuffer Execute(RuntimeEnvironment re)
	{
		StringBuffer result = new StringBuffer();
		
		try {
			//this.Do(re);
			
			this.AppendPostList(re);
			
			SVoid prop = null;
			
			if ((prop = this.m_properties.get("id")) == null)
			{
				throw new Exception("Critical property \"id\" is missing.");
			}
			
			String sid = prop.GetString(re);
			
			StringBuffer properties = new StringBuffer();
			if ((prop = this.m_properties.get("class")) != null) {
				properties.append(" class=\"");
				properties.append(prop.GetString(re));
				properties.append("\"");
			}
			if ((prop = this.m_properties.get("style")) != null) {
				properties.append(" style=\"");
				properties.append(prop.GetString(re));
				properties.append("\"");
			}
			
			result.append("<span id=\"" + sid + "\"" + properties + ">");
			if ((prop = this.m_properties.get("value")) == null) {
				ListIterator<Node> interator = this.m_children.listIterator();
				
		        while (interator.hasNext())
		        {
		        	result.append(interator.next().Execute(re));
		        }
			}
			else {
				result.append(prop.GetString(re));
			}
			result.append("</span>");

		} catch (Exception e) {
			SLog.println("Webpage runtime error on line " + this.m_currLineNumber);
			e.printStackTrace();
		}
		
		return result;
	}
	
	public void AppendPostList(RuntimeEnvironment re)
	{
		String sid = this.m_properties.get("id").GetString(re);
		
		re.AppendPostList("if($(\"" + sid + "\")!=null){");
		re.AppendPostList("para.add(\"cid\", \"" + sid + "\");");
		re.AppendPostList("para.add(\"type\", \"TextField\");");
		re.AppendPostList("para.add(\"" + sid + "_value\", $(\"" + sid + "\").innerHTML);");
		
		SVoid postback = this.GetProperty("postback");
		if (postback != null)
			re.AppendPostList(sid + "_postback", "\"" + SString.ToStringLiterealSafe(postback.GetString(re)) + "\"");
		
		re.AppendPostList("};");
	}
	
	public String GetText(RuntimeEnvironment re)
	{
		return this.m_properties.get("value").GetString(re);
	}
	
	public void SetText(RuntimeEnvironment re, String s)
	{
		this.m_properties.put("value", new SString(s));
		
		re.AppendJavascript("$(\"" + this.m_properties.get("id").GetString(re) + "\").innerHTML=\"" + this.m_properties.get("value").GetString(re) + "\";");
	}
	
	public NodeTextField ClearChildren(RuntimeEnvironment re)
	{
		//this.m_children.clear();
		
		re.AppendJavascript("$(\"" + this.m_properties.get("id").GetString(re) + "\").innerHTML=\"\";");
		
		return this;
	}
	
	public NodeTextField AppendChild(RuntimeEnvironment re, Node o)
	{
		//this.AddChild(o);
		
		re.GetRoot().AddNodeToIdMap(re, o);
		re.GetRoot().AddNodeToTypeList(re, o);
		
		re.AppendJavascript("$(\"" + this.m_properties.get("id").GetString(re) + "\").innerHTML+=\"" + SString.ToStringLiterealSafe(o.Execute(re)) + "\";");
		
		return this;
	}

}
